(function($, window) {
	var ocr = {
		errorCount:0,
		
		imageCount:100,
		
		init: function() {
			
			$("#btnGetImage").click(ocr.getImage);
			
			$("#btnListImages").click(ocr.listImage);
			
			$("#divResult").delegate(".image-container", "click", function(){
				if($(this).hasClass("image-container-error")){
					$(this).removeClass("image-container-error");
					ocr.errorCount--;
				}else{
					$(this).addClass("image-container-error");
					ocr.errorCount++;
				}
				var accuracy=(ocr.imageCount-ocr.errorCount)/ocr.imageCount*100;
				$("#accuracy").val(accuracy);
			});
			
			$(document).ajaxComplete(function(event,xhr,settings){
				setTimeout(function(){					
					$(".loading").hide();
				},600);
			}).ajaxSend(function(evt, request, settings){
				$(".loading").show();
			});
			
		},
		
		getImage: function() {
			var timestamp=ocr.getTimestamp();
			$.ajax({
				type: "GET",
				url: "engine/pbc/image",
				data:null,
				cache:false,// 禁止缓存
				success: function(data) {
					var sHtml=[];
					var imagebase64="data:image/png;base64,"+data.image;
					sHtml.push("<div class='image-container'>");
					sHtml.push("	<div>");			
					sHtml.push("		<img src='"+imagebase64+"'/>");
					sHtml.push("	</div>");	
					sHtml.push("	<div class='image-container-label'>"+data.text+"</div>");	
					sHtml.push("</div>");
					$(sHtml.join("")).appendTo($("#divResult"));
					ocr.imageCount++;
				}
			});
		},
		
		listImage: function() {
			var timestamp=ocr.getTimestamp();
			$.ajax({
				type: "GET",
				url: "engine/batch",
				data:null,
				cache:false,// 禁止缓存
				success: function(data) {
					var sHtml=[];
					ocr.imageCount=data.length;
					$.each(data,function(i,item){
						var imagebase64="data:image/png;base64,"+item.image;
						sHtml.push("<div class='image-container'>");
						sHtml.push("	<div>");			
						sHtml.push("		<img src='"+imagebase64+"'/>");
						sHtml.push("	</div>");	
						sHtml.push("	<div class='image-container-label'>"+item.text+"</div>");	
						sHtml.push("</div>");
					});
					$("#divResult").html(sHtml.join(""));
				}
			});
		},
		
		getTimestamp:function(){
			var timestamp=Math.round(new Date().getTime()/1000);
			return timestamp;
		}
	};
	
	window.ocr = (window.ocr) ? window.ocr : ocr;
})($, window);

$(function(){
	ocr.init();
});
